from django.db import connection
from django.shortcuts import render,redirect


# Create your views here.


def show(request):
    with connection.cursor() as c:
        c.execute('select * from student')
        raw = c.fetchall()
        for i in raw:
            print(i)
    return render(request, 'index.html', {'list': raw})


def add(request):
    if request.method == 'POST':
        snum = request.POST.get('snum')
        sname = request.POST.get('sname')
        with connection.cursor() as c:
            c.execute(f"insert into student values (NULL,{snum},'{sname}')")
        return redirect('index')
    return render(request, 'add.html')

def updates(request, id):
    with connection.cursor() as c:
        c.execute(f'select * from student where id={id}')
        list = c.fetchone()
        print(list)
    if request.method == 'POST':
        snum = request.POST.get('snum')
        sname = request.POST.get('sname')
        with connection.cursor() as b:
            b.execute(f'update student set snum={snum},sname="{sname}" where id={id}')
        return redirect('index')
    return render(request, 'updates.html', {"list": list})

def dels(request, id):
    with connection.cursor() as c:
        c.execute(f'delete from student where id={id}')
    return redirect('index')






